package com.sisgesbyte.dao.ventas;

import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.sisgesbyte.dao.beanDao.FiltrosSQLBean;
import com.sisgesbyte.dao.beanDao.ZonaRequest;
import com.sisgesbyte.dao.utilDao.CreaFiltroSqlString;
import com.sisgesbyte.data.entity.comun.GenericEntity;
import com.sisgesbyte.data.entity.ventas.ZonaEntity;
import com.sisgesbyte.data.util.AbstractFacade;

@Stateless
public class MantenimientoZonasDaoImpl extends AbstractFacade<ZonaEntity>  implements MantenimientoZonasDao {

	@Inject
	private CreaFiltroSqlString creaFiltroSqlString;
	
	@PersistenceContext(unitName = "SigesSiscompPU")
	private EntityManager em;

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}
	
	public MantenimientoZonasDaoImpl(){
		super(ZonaEntity.class);
	}
	
	
	@Override
	public List<ZonaEntity> listaZonas(FiltrosSQLBean cadenaFiltros) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("SELECT id, codigo, descripcion, active,banda_proyeccion FROM ventas.zona ");
		// concatenamos el filtro
		if (cadenaFiltros != null) {
			StringBuilder filtro = new StringBuilder();
			filtro.append(creaFiltroSqlString.concatenar(cadenaFiltros));
			jpql.append(filtro.toString());
		}
		Query query = em.createNativeQuery(jpql.toString(), ZonaEntity.class);
		if (cadenaFiltros != null && cadenaFiltros.getListaValores() != null
				&& cadenaFiltros.getListaValores().size() > 0) {
			int num = 0;
			for (Object valor : cadenaFiltros.getListaValores()) {
				query.setParameter(++num, valor);
			}
		}
		List<ZonaEntity> ls = query.getResultList();

		return ls;
	}

	@Override
	public GenericEntity guardarZona(ZonaRequest request) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("SELECT *  from ventas.sp_registrar_zona(:in_codigo, :in_descripcion, :in_active)");
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
			query.setParameter("in_codigo", request.getCodigo());
			query.setParameter("in_descripcion", request.getDescripcion());
			query.setParameter("in_active", request.getActive());
		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity actualizarZona(ZonaRequest request) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from ventas.sp_actualizar_zona(:in_id , :in_codigo, :in_descripcion, :in_active) ");
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_id", request.getId());
		query.setParameter("in_codigo", request.getCodigo());
		query.setParameter("in_descripcion", request.getDescripcion());
		query.setParameter("in_active", request.getActive());
		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity eliminarZona(int codZona) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from ventas.sp_eliminar_zona(:in_id)");
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
			query.setParameter("in_id", codZona);
		return (GenericEntity) query.getSingleResult();
	}
}
